package com.exchange.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.exchange.common.model.dto.Tick;
import com.exchange.common.model.dto.Trade;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

import cn.hutool.core.util.NumberUtil;

public class KlineUtil {
	
	private static Cache rides = Redis.use(PropKit.get("redisName"));


    public static final String min_1 = "1m";
    public static final String min_5 = "5m";
    public static final String min_15 = "15m";
    public static final String min_30 = "30m";
    public static final String hour_1 = "60m";
    public static final String hour_4 = "240m";
    public static final String hour_8 = "480m";
    public static final String day_1 = "1d";
    public static final String week_1 = "1w";

    public static final Map<String, Long> klineTypes = new ConcurrentHashMap<>();
    public static final List<String> types = new ArrayList<>();
    public final static Map<String, Tick> KLines = new ConcurrentHashMap<>();
    
    static {
        types.add(min_1);
        types.add(min_5);
        types.add(min_15);
        types.add(min_30);
        types.add(hour_1);
        types.add(hour_8);
        types.add(day_1);
        types.add(hour_4);
        types.add(week_1);
        
        
        
        klineTypes.put(min_1, 60 * 1000L);
        klineTypes.put(min_5, 5 * 60 * 1000L);
        klineTypes.put(min_15, 15 * 60 * 1000L);
        klineTypes.put(min_30, 30 * 60 * 1000L);
        klineTypes.put(hour_1, 60 * 60 * 1000L);
        klineTypes.put(hour_8, 8 * 60 * 60 * 1000L);
        klineTypes.put(day_1, 24 * 60 * 60 * 1000L);
        klineTypes.put(week_1, 7 * 24 * 60 * 60 * 1000L);
        klineTypes.put(hour_4, 4 * 60 * 60 * 1000L);

    }

    
    /**
     * 放入 k 线
     * 用redis存最新的柱
     */
    public static void put(Trade trade, long timestamp) {
    	
    	for(String key : klineTypes.keySet()){
    		Tick tick = rides.get("newTick:"+trade.getCoinTeam()+":"+key);
        	if( timestamp - tick.getTime() < klineTypes.get(key)) {//如果这次传入的时间-上一次的时间<1分钟（一分钟存一次时序）
        		//更新这个柱的信息
        		if(NumberUtil.isGreater(trade.getPrice(), tick.getHigh())){//>
        			tick.setHigh(trade.getPrice());
        		};
        		if(NumberUtil.isGreater(tick.getLow(), trade.getPrice())){//>
        			tick.setLow(trade.getPrice());
        		};
        		tick.setVol(tick.getVol().add(trade.getNumber()));
        		tick.setClose(trade.getPrice());
        		rides.set("newTick:"+trade.getCoinTeam()+":"+key, tick);
        	}else {
    			//替换这个柱
        		Tick newTick = new Tick();
        		newTick.setOpen(trade.getPrice());
        		newTick.setHigh(trade.getPrice());
        		newTick.setLow(trade.getPrice());
        		newTick.setClose(trade.getPrice());
        		newTick.setVol(trade.getNumber());
        		newTick.setTime(getTimestampByType(timestamp, key));
        		rides.set("newTick:"+trade.getCoinTeam()+":"+key, newTick);
    		}
    	}
    	
    }	

    private static long getTimestampByType(long timestamp, String type) {
        return timestamp - timestamp % klineTypes.get(type);
    }


	public static Map<String, Long> getKlinetypes() {
		return klineTypes;
	}


	public static List<String> getTypes() {
		return types;
	}


	public static Map<String, Tick> getKlines() {
		return KLines;
	}

    
    
    
    
    

}
